﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class ReportLadderModel
    {
        public List<StorageService.Report> reports = new List<StorageService.Report>();

        public List<string> years = new List<string>();
        public List<List<string>> months = new List<List<string>>();
        public List<List<List<string>>> days = new List<List<List<string>>>();

        public int counter = 0;

        public ReportLadderModel()
        {

        }

        /// <summary>
        /// Finds a list of distinct years in all reports.
        /// </summary>
        /// <returns>List of distinct years.</returns>
        public List<string> GetYears()
        {
            List<string> distinct = new List<string>();
            for (int i = 0; i < reports.Count(); i++)
            {
                distinct.Add("" + reports[i].Modified.DateTime.Year);
            }
            distinct = distinct.Distinct().ToList();

            return distinct;
        }

        /// <summary>
        /// Finds a list of distinct months in all reports.
        /// </summary>
        /// <returns>List of distinct months.</returns>
        public List<List<string>> GetMonths()
        {
            List<List<string>> compiled = new List<List<string>>();
            for (int i = 0; i < years.Count(); i++)
            {
                List<string> distinct = new List<string>();
                for (int a = 0; a < reports.Count(); a++)
                {
                    if (reports[a].Modified.DateTime.Year == Convert.ToInt32(years[i]))
                    {
                        distinct.Add("" + reports[a].Modified.DateTime.Month);
                    }
                }
                distinct = distinct.Distinct().ToList();
                compiled.Add(distinct);
            }

            return compiled;
        }

        /// <summary>
        /// Finds a list of distinct days in all reports.
        /// </summary>
        /// <returns>List of distinct days.</returns>
        public List<List<List<string>>> GetDays()
        {
            List<List<List<string>>> ultraCompiled = new List<List<List<string>>>();
            for (int i = 0; i < years.Count(); i++)
            {
                List<List<string>> compiled = new List<List<string>>();
                for (int a = 0; a < months[i].Count(); a++)
                {
                    List<string> distinct = new List<string>();
                    for (int b = 0; b < reports.Count(); b++)
                    {
                        if (reports[b].Modified.DateTime.Year == Convert.ToInt32(years[i]) && reports[b].Modified.DateTime.Month == Convert.ToInt32(months[i][a]))
                        {
                            distinct.Add(reports[b].Modified.DateTime.ToString("MM/dd/yyyy"));
                        }
                    }
                    distinct = distinct.Distinct().ToList();
                    compiled.Add(distinct);
                }
                ultraCompiled.Add(compiled);
            }

            return ultraCompiled;
        }

        public string ParseMonth(string month)
        {
            if (month == "1")
            {
                return "January";
            }
            else if (month == "2")
            {
                return "February";
            }
            else if (month == "3")
            {
                return "March";
            }
            else if (month == "4")
            {
                return "April";
            }
            else if (month == "5")
            {
                return "May";
            }
            else if (month == "6")
            {
                return "June";
            }
            else if (month == "7")
            {
                return "July";
            }
            else if (month == "8")
            {
                return "August";
            }
            else if (month == "9")
            {
                return "September";
            }
            else if (month == "10")
            {
                return "October";
            }
            else if (month == "11")
            {
                return "November";
            }
            else
            {
                return "December";
            }
        }

        public List<StorageService.Report> GetRelevantReports(int year, int month, string day)
        {
            List<StorageService.Report> final = new List<StorageService.Report>();
            int dayInt = Convert.ToInt32(day.Substring(3, 2));

            for (int i = 0; i < reports.Count(); i++)
            {
                if (reports[i].Modified.DateTime.Year == year && reports[i].Modified.DateTime.Month == month && reports[i].Modified.DateTime.Day == dayInt)
                {
                    final.Add(reports[i]);
                }
            }
            return final;
        }
    }
}